// pages/process/process-finish/process-finish.js
import { toast } from '../../../../utils/Tools.js'
import moment from '../../../../utils/moment'
import {debounce} from '../../../../utils/Tools'
const app = getApp()
Page({
  data: {
    id:'',
    pics: [],
    time:'',
    get_address:'',
    marks:'',
    carName:''
  },
  onLoad: function (options) {
    const {id,address} = options
    const time = moment(new Date(),'YYYY-MM-DD hh:mm:ss')
    this.setData({
      time,
      id,
      get_address: address || '点击获取配送地点'
    })
    this.handleFormInput = debounce(this.handleFormInput)
    wx.hideHomeButton()
  },
  handleFormInput(e){   //  处理表单输入
    const {type} = e.currentTarget.dataset
    this.setData({
      [type]: e.detail.value
    })
  },
  handleAddress(){  //点击配送地点
    wx.navigateTo({
      url: '/driver_side/pages/process/process-map/process-map?id='+this.data.id
    });
  },
  //上传图片开始
  chooseImg: function (e) {
    var that = this, pics = this.data.pics;
    if (pics.length < 11) {
      wx.chooseImage({
        count: 10, // 最多可以选择的图片张数，默认9
        success: function (res) {
          var tempFilePaths = res.tempFilePaths;
          for (var i = 0; i < tempFilePaths.length;i++){
            pics.push(tempFilePaths[i]);
          }
          that.setData({
            pics
          })
        },
      });
    } else {
      toast('最多上传10张图片',null,3000)
    }
  },
   // 预览图片
  previewImg: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var pics = this.data.pics;
    wx.previewImage({
      //当前显示图片
      current: pics[index],
      //所有图片
      urls: pics
    })
  },

  // 删除图片
  deleteImg: function (e) {
    var pics = this.data.pics;
    var index = e.currentTarget.dataset.index;
    pics.splice(index, 1);
    this.setData({
      pics
    })
  },

  //      网络请求
  handleSubmit(){
    console.log(this.data.id)
    let tokens=wx.getStorageSync('token');
    if(this.data.get_address&&this.data.pics.length){
      for(let i=0;i<this.data.pics.length;i++){
        wx.uploadFile({
          url: app.globalData.BASE_URL+'api/driver/order/remarks',
          filePath: this.data.pics[i],
          name: 'remarks_pic',
          formData: {
            order_id:this.data.id,
            remarks:  this.data.marks,
            end_address: this.data.get_address,
            token: tokens
          }, 
          header: {
            "Content-Type": "multipart/form-data"
          },
          success: res => {
            console.log(res,'99行')
            const result = JSON.parse(res.data)
            // const result=res.data;
            if(result.code ==200){
              toast('上传成功','success',2000)
              setTimeout(()=>{
                wx.reLaunch({
                  url: '/driver_side/pages/process/index/index',
                })
              },2000)
            }else{
              toast('上传失败',null,2000)
            }
          },
          fail: err =>{
            console.log(err)
          }
        })
      }    
    }else{
      toast('提交失败,请先填写内容或者上传图片')
      return;
    }  
  }
})